#include "draw2d.h"
#include "tgaimage.h"
#include "model.h"

int main(int argc,char** argv)
{
    TGAImage *image=new TGAImage(800,600,3);
    Model *model;
    Draw2d* draw;
    Vec3f light_dir(0.,0.,-1.);
    if(argc==2)
    {
        model=new Model(argv[1]);
    }
    else 
    {
        model=new Model("obj/african_head.obj");
    }
    for(int i=0;i<model->nfaces();i++)
    {
        std::vector<int> face=model->face(i);
        Vec2i screen_coords[3];
        Vec3f world_coords[3];
        for(int j=0;j<3;j++)
        {
            Vec3f v=model->vert(face[j]);
            screen_coords[j]=Vec2i((v.x+1.)*400.,(v.y+1.)*300.);
            world_coords[j]=v;
        }
        Vec3f n=(world_coords[2]-world_coords[0])^(world_coords[1]-world_coords[0]);
        n.normalize();
        float cosTheta=n*light_dir;
        float intensity=1.*cosTheta;
        if(intensity>0)
        {
            draw->fillcolor(image,screen_coords[0],screen_coords[1],screen_coords[2],TGAColor(255*intensity,255*intensity,255*intensity,255));
        }
    }
    image->flip_vertically();
    image->write_tga_file("wireframe_fill_2.tga");
    return 0;
}